package coco.usbhid

import coco.usbhid.UsbManage.keyReport
import coco.usbhid.UsbManage.sendData
import coco.usbhid.UsbManage.touchReport
import com.UsbPlugins
import com.yanzhenjie.andserver.annotation.GetMapping
import com.yanzhenjie.andserver.annotation.RequestMapping
import com.yanzhenjie.andserver.annotation.RequestParam
import com.yanzhenjie.andserver.annotation.RestController
import java.lang.Thread.sleep
//http://172.16.1.4:8085/connect
@RestController
@RequestMapping("/")
class Env {
    @GetMapping("/connect")
    fun connect(
    ): Boolean {
        UsbManage.connect()
        return true

    }
}
//http://172.16.1.4:8085/keyboard/write?k=4
@RestController
@RequestMapping("/keyboard")
class KeyBoard {
    @GetMapping("/paste")
    fun paste(): Boolean {
        return UsbPlugins.paste()
    }

    @GetMapping("/copy")
    fun copy(): Boolean {

        return UsbPlugins.copy()
    }

    @GetMapping("/home")

    fun home(): Boolean {

        return UsbPlugins.home()
    }

    @GetMapping("/back")
    fun back(): Boolean {

        return UsbPlugins.back()
    }

    @GetMapping("/enter")
    fun enter(): Boolean {
        return UsbPlugins.enter()
    }

    @GetMapping("/recent")
    fun recent(): Boolean {
        return UsbPlugins.recent()
    }


//    @GetMapping("/release")
//    fun release(): Boolean {
//        return UsbPlugins.release()
//    }
//
//    @GetMapping("/press")
//    fun press(@RequestParam("modifiers") modifiers: String,@RequestParam("key")key:String): Boolean {
//        val modifiersInt = modifiers.removePrefix("0x").toInt(16)
//        val keyInt = key.removePrefix("0x").toInt(16)
//        return UsbPlugins.press(modifiersInt,keyInt)
//
//    }


    @GetMapping("/write")
    fun write(@RequestParam("modifiers") modifiers: String,@RequestParam("key")key:String): Boolean {
        val modifiersInt = modifiers.removePrefix("0x").toInt(16)
        val keyInt = key.removePrefix("0x").toInt(16)

        return UsbPlugins.write(modifiersInt,keyInt)
    }

}
//http://172.16.1.4:8085/mouse/click?x=200&y=300
@RestController
@RequestMapping("/mouse")
class Mouse {
    @GetMapping("/click")
    fun click(
        @RequestParam("x") x: Int,
        @RequestParam("y") y: Int,
    ): Boolean {
      UsbManage.click(x.toDouble(),y.toDouble())
        return true

    }


    @GetMapping("/swipe")
    fun swipe(
        @RequestParam("x") x: Double,
        @RequestParam("y") y: Double,
        @RequestParam("ex") ex: Double,
        @RequestParam("ey") ey: Double
    ): Boolean {

        return  UsbManage.swipe(x, y, ex, ey)
    }
    @GetMapping("/swipe1")
    fun swipe1(
        @RequestParam("x") x: Double,
        @RequestParam("y") y: Double,
        @RequestParam("ex") ex: Double,
        @RequestParam("ey") ey: Double,
        @RequestParam("s")  s: Double ,
    ): Boolean {
        return  UsbManage.swipe1(x, y, ex, ey,s,1000)
    }

    @GetMapping("/release")
    fun release(
    ): Boolean {

        return UsbManage.release()
    }

    @GetMapping("/press")
    fun press(
        @RequestParam("x") x: Double,
        @RequestParam("y") y: Double,
    ): Boolean {
        return UsbManage.press(x, y)
    }

}
